package com.chuanjiabao.wisdom.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chuanjiabao.wisdom.MainActivity;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.InputBean;
import com.chuanjiabao.wisdom.databinding.FragmentInputPersionFirstBinding;
import com.chuanjiabao.wisdom.listener.OnBHClickListener;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.view.DialogGLC;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpUtils;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.TimeUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.chuanjiabao.wisdom.util.dialog.SelectSexDialog;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class InputPersionFirstFramgment extends BaseLazyFragment {

    FragmentInputPersionFirstBinding binding;
    DialogGLC mDialog;
    InputBean inputBean ;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_input_persion_first;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(
                LayoutInflater.from(getActivity()),
                R.layout.fragment_input_persion_first,
                container,
                false
        );
        binding.setFragment(this);
        return binding.getRoot();
    }

    @Override
    protected void onVisible() {
        String name = binding.etModuleTrueName.getText().toString().trim();
        InputBean inputBean = new InputBean();
        if (!TextUtils.isEmpty(name)) {
            inputBean.setName(name);
        }

        String birthday = binding.tvModuleSelectDay.getText().toString().trim();
        if (!TextUtils.isEmpty(birthday)) {
            inputBean.setBirthday(birthday);
        }

        String sex = binding.tvModuleSelectSex.getText().toString().trim();
        if (!TextUtils.isEmpty(sex)) {
            inputBean.setSex(sex);
        }

        SpUtils.putBean("module_inputBean",inputBean);
    }

    @Override
    protected void lazyLoad() {
        try{
            inputBean = (InputBean) SpUtils.getBean("module_inputBean");
            binding.tvModuleSelectSex.setText(inputBean.getSex());
            binding.tvModuleSelectDay.setText(inputBean.getBirthday());
            binding.etModuleTrueName.setText(inputBean.getName());
        }catch (Exception e){
            e.printStackTrace();
        }

        mDialog = new DialogGLC(getActivity());
        mDialog.setCancelable(true);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.initCalendar();

        binding.tvModuleOk.setOnClickListener(this);
        binding.tvModuleSelectDay.setOnClickListener(this);
        binding.llModuleSelectSex.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvModuleSelectDay:
                if (mDialog.isShowing()) return;

                mDialog.show();
                mDialog.setOnDataSelectListener((yangData, yinData, index) -> {
                    switch (index) {
                        case 0:
                            birthday_type = "1";
                            birthday = TimeUtil.timeToStr(yangData);
                            binding.tvModuleSelectDay.setText(yangData + "(阳历)");
                            break;
                        case 1:
                            birthday_type = "2";
                            birthday = TimeUtil.timeToStr(yinData);
                            binding.tvModuleSelectDay.setText(yinData + "(阴历)");
                            break;
                    }
                });
                break;
            case R.id.llModuleSelectSex:
                SelectSexDialog dialog = new SelectSexDialog(getActivity());
                dialog.show();
                dialog.setOnBHClickListener(new OnBHClickListener() {
                    @Override
                    public void onConfirmClick() {
                        sex = "2";
                        binding.tvModuleSelectSex.setText("女");
                    }

                    @Override
                    public void onCloseClick() {
                        sex = "1";
                        binding.tvModuleSelectSex.setText("男");
                    }
                });
                break;
            case R.id.tvModuleOk:
                truename = binding.etModuleTrueName.getText().toString().trim();
                if (TextUtils.isEmpty(truename)) {
                    ToastUtil.showToast("请输入姓名");
                    return;
                }

                if (TextUtils.isEmpty(birthday)) {
                    ToastUtil.showToast("请选择生日");
                    return;
                }

                if (TextUtils.isEmpty(sex)) {
                    ToastUtil.showToast("请输入性别");
                    return;
                }

                udpateUserInfo();
                break;
        }
    }

    /**
     * 修改/提交个人信息
     */
    String sex = "1",
            birthday = "",
            birthday_type = "",
            truename = "",
            province = "",
            city = "",
            district = "",
            address = "",
            avatar = "";

    public void udpateUserInfo() {
        HTTPAPI.getInstance().udpateUserInfo(sex, birthday, birthday_type,
                truename, province, city, district, address, avatar,
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (!StringUtil.isFail(s)) {
                            StringUtil.showFail(getActivity(), s);
                        } else {
                            startActivity(new Intent(getActivity(), MainActivity.class));
                            ActivityStack.getScreenManager().popAllActivityExceptOne(MainActivity.class);
                        }
                    }
                });
    }

}
